package com.lxw.demo.login.controller;

import com.lxw.demo.annotation.Auth;
import com.lxw.demo.config.ResultCode;
import com.lxw.demo.config.ResultJson;
import com.lxw.demo.login.domain.User;
import com.lxw.demo.login.service.Impl.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;
import java.util.UUID;

@RestController
@RequestMapping("/noToken")
public class UserController {
    @Value("${token.prefix}")
    private String prefix;


    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private UserService userService;


    @PostMapping("login")
    public ResultJson login(@RequestBody User user){
/*        User user = new User();
        user.setAccount(123456);
        user.setPassword("123");
        String token = UUID.randomUUID() + "";
        if(Objects.equals(User.getAccount(), user.getAccount()) && User.getPassword().equals(user.getPassword())){
//            redisTemplate.delete("123");
            redisTemplate.opsForValue().set(User.getAccount().toString(),token);


            return ResultJson.ok(ResultCode.SUCCESS);
        }else{
            return ResultJson.failure(ResultCode.LOGIN_ERROR);
        }*/

        List<User> userTrue = userService.userLogin(user.getAccount());
        if(user.getAccount().equals(userTrue.get(0).getAccount()) && user.getPassword().equals(userTrue.get(0).getPassword())){
            String token = prefix + UUID.randomUUID() + "";
            System.out.println(token);
            redisTemplate.opsForHash().put("Authorization", token,userTrue.get(0).getId());
            return ResultJson.ok(ResultCode.SUCCESS);
        }else{
            return ResultJson.failure(ResultCode.LOGIN_ERROR);
        }


    }


    @GetMapping("test1")
    public ResultJson test(Integer account){
        System.out.println("success");
        return ResultJson.ok(ResultCode.SUCCESS);
    }
    @GetMapping("test2")
    @Auth("check")
    public ResultJson test2(Integer account){
        System.out.println("success");
        return ResultJson.ok(ResultCode.SUCCESS);
    }
    @GetMapping("test3")
    @Auth("update")
    public ResultJson test3(Integer account){
        System.out.println("success");
        return ResultJson.ok(ResultCode.SUCCESS);
    }





}

